package com.itheima.bos.web.action.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.service.base.CourierService;
import com.itheima.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/*
 * 快递员操作方法
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class CourierAction extends BaseAction<Courier> {

	@Autowired
	private CourierService courierService;

	// 添加快递员方法
	@Action(value = "courier_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}

	// 分页查询快递员信息

	@Action(value = "courier_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {

		Specification<Courier> spec = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

				List<Predicate> list = new ArrayList<Predicate>();

				// 单表查询(即我们根据条件查询)
				if (StringUtils.isNoneBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(root.get("courierNum").as(String.class), model.getCourierNum());
					list.add(p1);
				}
				if (StringUtils.isNoneBlank(model.getCompany())) {
					Predicate p2 = cb.like(root.get("company").as(String.class), "%" + model.getCompany() + "%");
					list.add(p2);
				}
				if (StringUtils.isNoneBlank(model.getType())) {
					Predicate p3 = cb.equal(root.get("type").as(String.class), model.getType());
					list.add(p3);
				}
				// 查询的条件是关联其它表的字段
				Join<Object, Object> standardRoot = root.join("standard", JoinType.INNER);
				if (model.getStandard() != null && StringUtils.isNoneBlank(model.getStandard().getName())) {
					Predicate p4 = cb.like(standardRoot.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					list.add(p4);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}
		};

		// 封装page和rows
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Courier> pageData = courierService.findPageData(spec, pageable);

		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());

		ActionContext.getContext().getValueStack().push(result);

		return SUCCESS;
	}

	/*
	 * 修改快递员是否作废
	 */
	private String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Action(value = "courier_delBatch", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String delBatch() {

		String[] idArray = ids.split(",");
		courierService.delBatch(idArray);

		return SUCCESS;
	}

	@Action(value = "courier_restore", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/courier.html") })
	public String restore() {

		String[] idArray = ids.split(",");
		courierService.restore(idArray);

		return SUCCESS;
	}

	/*
	 * 查找出没有关联定区的快递员
	 */
	@Action(value = "courier_findnoassociation", results = { @Result(name = "success", type = "json") })
	public String findnoassociation() {
		
		List<Courier> couriers =  courierService.findnoassociation();
		
		ActionContext.getContext().getValueStack().push(couriers);
		return SUCCESS;
	}
}
